#include <iostream>
#include <string>
#include <boost/asio.hpp>

#include "ServicePool.h"
#include "Server.h"
#include "MessageQueue.h"
#include "Trace.h"

using namespace boost::asio;
using namespace tsc::common;

int main(int argc, char* argv[])
{
  ENABLE_ALL_TRACE();
  TRACE_PROCESS("server_tcp");

  ENTER("main");
  try
  {
    if (argc != 3)
    {
      TRACE_ERROR("Usage: %s <address> <port>\n    %s 127.0.0.1 1080",
                argv[0], argv[0]);
      RETURN(1);
    }

    ServicePool pool;

    io_service& service = pool.getService();


    Server server(argv[1], argv[2], service);

    INFO("Server application starting.");
    pool.run();
  }
  catch (std::exception& exc)
  {
	  TRACE_ERROR("Server application cought exception: %s", exc.what());
  }

  INFO("Server flushing messege queue.");
  MessageQueue::instance()->~MessageQueue();
  INFO("Server application finished.");
  RETURN(0);
}
